package com.wyz.module.common.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分页转换工具
 * @version 1.0
 * @author hejing
 * @date 2024/4/15 10:10:02
 */
public class ConvertUtil {
    private ConvertUtil(){

    }

    public static <T, R> Page<R> convert(Page<T> source, Function<T, R> converter) {
        List<R> records = source.getRecords().stream().map(converter).collect(Collectors.toList());
        Page page = new Page<>(source.getCurrent(), source.getSize(), source.getTotal());
        page.setRecords(records);
        return page;
    }

    public static <T, R> List<R> convertList(List<T> source, Function<T, R> converter) {
        return source.stream().map(converter).collect(Collectors.toList());
    }
}
